<?php
/**
 * 广告位管理
 * TODO 优化广告显示管理
 * @author Ice <iceinto@mallog.com.cn>
 */
class AdAction extends LoginAction
{
	private $type = 'ad';

	public function index()
	{
		$market = M('Market')->field('id,name')->select();
		$AdModel = D('Ad');
		$map = array();
		//获取搜索参数
		$mId = intval(getGet('market'));
		$fId = intval(getGet('floor'));
		$bId = intval(getGet('brand'));
		$mall = $this->getMarket($mId);
		$floor = $this->getFloor($fId);
		$brand = $this->getBrand($bId);
		//获取广告数据
		if ($mId) {
			$map['m_ad_allot.mid'] = $mId;
			$map['m_ad_allot.fid'] = $fId;
			$map['m_ad_allot.bid'] = $bId;
		}
		$ad = $AdModel->getAllPro($map);
		foreach ($ad['data'] as $k => $v) {
			$ad['data'][$k]['imageUrl'] = $this->getManagedUrl($v['image'], $this->type);
		}
		$this->assign('ad', $ad['data']);
		$this->assign('page', $ad['page']);
		$this->assign('market', $market);
		$this->assign('mall', $mall);
		$this->assign('floor', $floor);
		$this->assign('brand', $brand);
		$this->display();
	}

	/**
	 * 没有显示广告图
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function noshow()
	{
		$AdModel = D('Ad');
		$ad = $AdModel->getNoShow();
		foreach ($ad as $k => $v) {
			$ad[$k]['imageUrl'] = $this->getManagedUrl($v['image'], $this->type);
		}
		$this->assign('ad', $ad);
		$this->display();
	}

	/**
	 * 添加广告
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function add()
	{
		if (isPost()) {
			$AdModel = D('Ad');
			$data = $this->getPostData();
			$fId = $this->uploadFiles($this->type);
			if ($fId) {
				$data['image'] = $fId;
			}
			$crs = $AdModel->create($data);
			if ($crs) {
				$rs = $AdModel->add();
				if ($rs) {
					$this->successMessage('添加成功！');
				} else {
					$this->error('添加失败！');
				}
			} else {
				$this->error($AdModel->getError());
			}
		}
		$this->getSetData();
		$this->display();
	}

	/**
	 * 管理广告显示位置
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function show()
	{
		$id = intval(getGet('id'));
		$map = array();
		$map['id'] = $id;
		$ad = $this->setBaseData('Ad', $map, 'ad');
		if (isPost()) {
			$data = $this->getPostShowData();
			$this->showAd($ad, $data);
			$ad = $this->setBaseData('Ad', $map, 'ad');
		}
		if ($ad) {
			$imageUrl = $this->getManagedUrl($ad['image'], $this->type);
			$this->assign('imageUrl', $imageUrl);
		}
		$this->setAdShowData($ad);
		//判断当前是否有显示配置 显示等级（1首页，2商场，3首页&商场，4品牌）
		if ($ad['type'] == 5) {
			$this->display('slideShow');
		} else {
			$this->display();
		}
	}

	/**
	 * 管理广告显示位置移除
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function clearShow()
	{
		$id = intval(getGet('id'));
        if(!$id){
            $this->error('去除标示错误！');
        }
		$map = array();
		$map['id'] = $id;
		$adAllotModel = M('AdAllot');
		$rs = $adAllotModel->where($map)->delete();
		if ($rs) {
			$this->success('位置移除成功！');
		} else {
			$this->error('位置移除失败！');
		}
	}

	/**
	 * 修改广告
	 * @author Ice iceinto@mallog.com.cn
	 */
	public function edit()
	{
		$id = intval(getGet('id'));
		$map = array();
		$map['id'] = $id;
		if (isPost()) {
			$isUpLoad = false;
			$ad = $this->setBaseData('Ad', $map, 'ad');
			$data = $this->getPostData();
			if ($_FILES['image']['name']) {
				$fId = $this->uploadFiles($this->type);
				if ($fId) {
					$data['image'] = $fId;
					$isUpLoad = true;
				}
			}
			$data['id'] = $id;
			if ($this->createUpdate('Ad', $data, false) && $isUpLoad) {
				$this->delUpLoadFile($ad['image'], $this->type);
			}
		}
		$ad = $this->setBaseData('Ad', $map, 'ad');
		if ($ad) {
			$imageUrl = $this->getManagedUrl($ad['image'], $this->type);
			$this->assign('imageUrl', $imageUrl);
		}
		$this->getSetData();
		$this->display();
	}

	/**
	 * 设置基本参数
	 * @param Int $id 广告ID
	 * @author Ice iceinto@mallog.com.cn
	 */
	public function getSetData($id = null)
	{
		if ($id) {
			$map = array();
		}
		$this->setBaseData('adType', null, 'at', true);
	}

	/**
	 * 获取用户提交数据
	 * @author Ice iceinto@mallog.com.cn
	 */
	public function getPostData()
	{
		$data = array();
		$data['name'] = getPost('adName');
		$data['type'] = getPost('at');
		$data['keyword'] = getPost('keyword');
		return $data;
	}

	/**
	 * 设置显示等级选择
	 * @param Int $type 等级  1首页品牌大图  2品牌大图  3首页和品牌中图  4品牌小图
	 * @author Ice iceinto@mallog.com.cn
	 */
	public function setAdShowData($ad)
	{
		$type = $ad['type'];
		$market = M('Market')->field('id,name')->select();
		if ($type == 1 || $type == 2 || $type == 3 || $type == 4 || $type == 5) {
			//获取商场、楼层
			if ($type == 3) {
				$this->assign('mb', true);
			}
			if ($type == 5) {
				$this->assign('slide', true);
			}
		} else {
			$this->error('设置显示等级选择 - 类型数据有错误！');
		}
		$this->assign('market', $market);
		//设置有显示信息的数据
		$map = array();
		$map['id'] = $ad['id'];
		$adAllotModel = M('AdAllot');
		$aa = $adAllotModel->where($map)->find();
		if ($aa) {
			if ($aa['mid']) {
				unset($map);
				$map['id'] = $aa['mid'];
				$this->setBaseData('Market', $map, 'adMarket');
			}
			if ($aa['fid']) {
				unset($map);
				$map['id'] = $aa['fid'];
				$this->setBaseData('Floor', $map, 'floor');
			}
			if ($aa['bid']) {
				unset($map);
				$map['id'] = $aa['bid'];
				$this->setBaseData('Brand', $map, 'brand');
			}
			$levelDis = '';
			if ($aa['level'] == 1) {
				$levelDis = '首页';
			} else if ($aa['level'] == 2) {
				$levelDis = '商场';
			} else if ($aa['level'] == 3) {
				$levelDis = '首页&商场';
			} else if ($aa['level'] == 4) {
				$levelDis = '品牌';
			}
			$this->assign('levelDis', $levelDis);
		}
		$this->assign('adAllot', $aa);
	}

	/**
	 * 获取展示提交数据
	 * @return Array 提交数据整理
	 * @author Ice iceinto@mallog.com.cn
	 */
	private function getPostShowData()
	{
		$data = array();
		$data['id'] = getGetOrPost('id');
		$data['mid'] = getPost('market');
		$data['fid'] = getPost('floor');
		$data['bid'] = getPost('brand');
		$data['url'] = getPost('url');
		$data['level'] = intval(getPost('level'));
		return $data;
	}

	/**
	 *
	 * 执行广告显示操作
	 * @param Array $ad 广告信息
	 * @param Array $data 显示信息
	 * @author Ice iceinto@mallog.com.cn
	 */
	private function showAd($ad, $data)
	{
		$map = array();
		$map['id'] = $ad['id'];
		$adAllotModel = M('AdAllot');
		//判断广告位置是否已经存在
		$aa = $adAllotModel->where($map)->find();
		//进行等级设置 等级  1首页品牌大图  2品牌大图  3首页和品牌中图  4品牌小图
		if (!$data['mid']) {
			$this->error('所有广告商场必须！');
		}
		if ($ad['type'] == 1) {
			$data['level'] = 1;
			$data['fid'] = 0;
			$data['bid'] = 0;
		} else if ($ad['type'] == 2) {
			$data['level'] = 2;
		} else if ($ad['type'] == 4) {
			$data['level'] = 4;
		} else if ($ad['type'] == 5) {
			//进行滚动图判断
			if ($data['level'] == 1 || $data['level'] == 3) {
				if (!$data['mid']) {
					$this->error('至少选择商场！');
				}
				if ($data['fid'] && $data['bid']) {
				} else {
					$data['fid'] = 0;
					$data['bid'] = 0;
				}
			} else if ($data['level'] == 4) {
				if (!$data['mid'] || !$data['fid'] || !$data['bid']) {
					$this->error('选择品牌时候，商场、楼层和品牌必须选择！');
				}
			} else if ($data['level'] == 2) {
				if (!$data['mid'] || !$data['fid']) {
					$this->error('商场和楼层必须选择！');
				}
			} else {
				$this->error('执行广告显示操作 - 数据选择有错误！');
			}
		}
		if ($aa) {
			$this->createUpdate('AdAllot', $data);
		} else {
			$this->createAdd('AdAllot', $data);
		}
	}

	/**
	 * 滚动图管理
	 * @author Ice iceinto@mallog.com.cn
	 */
	public function slide(){
		if(isPost()){
			$ordery = getPost('ordery');
			$aid = getPost('aid');
			if(count($ordery) == count($aid)){
				$data = array();
				$AdAllotModel = M('AdAllot');
				foreach($aid as $key=>$id){
					$data['id'] = $id;
					$data['ordery'] = $ordery[$key];
					$AdAllotModel->save($data);
				}
			}else{
				$this->error('提供修改排序的数据不对！');
			}
		}
		$mid = intval(getGet('mid'));
		$malls = M('Market')->field('m_id,m_name')->select();
		if($mid){
			$this->setMallSlide($mid);
		}else{
			$this->setHomeSlide();
		}
		$this->assign('malls', $malls);
		$this->display();
	}

	/**
	 * 设置滚动图
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function setHomeSlide ()
	{
		$map = array();
		$AdModel = D('Ad');
		$map['m_ad.type'] = 5;
		$map['m_ad_allot.level'] = array(array('eq', 1), array('eq', 3), 'or');
		$slides = $AdModel->getAd($map, 'm_ad.*,m_ad_allot.*', 'm_ad_allot.ordery DESC');
		$this->assign('slides', $slides);
	}

	/**
	 * 设置商场滚动图
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	private function setMallSlide ($id)
	{
		$map = array();
		$AdModel = D('Ad');
		$map['m_ad.type'] = 5;
		$map['m_ad_allot.m_id'] = $id;
		$map['m_ad_allot.level'] = array(array('eq', 2), array('eq', 3), 'or');
		$slides = $AdModel->getAd($map, 'm_ad.*,m_ad_allot.*', 'm_ad_allot.ordery ASC');
		$this->assign('slides', $slides);
	}

	public function getMarket($id)
	{
		return M('Market')->field('id,name')->find($id);
	}

	public function getFloor($id)
	{
		return M('Floor')->field('id,name,sain')->find($id);
	}

	public function getBrand($id)
	{
		return M('Brand')->field('id,name')->find($id);
	}
}

?>
